package cn.hutool.postfix.generator.lang;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.lang.Tuple;
import cn.hutool.core.map.MapUtil;
import cn.hutool.postfix.generator.common.Config;

import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author liuxiongkang
 */
public abstract class BaseLang implements Lang {
	protected static final String ARR_FLAG = "[]";
	protected Map<String, Collection<String>> javaExcludes = Config.COMMON_CONFIG.getMap(lang() + "_excludes").entrySet().stream()//
			.map(x -> new Tuple(x.getKey(), CollUtil.edit(Arrays.asList(x.getValue().split(",")), String::trim))).collect(Collectors.toMap(x -> x.get(0), x -> x.get(1)));
	protected Map<String, Function<String, String>> typeConvert = MapUtil.of(//
			Pair.of("int", t -> "INT"),//
			Pair.of("java.lang.Integer", t -> "INT"),//
			Pair.of("long", t -> "LONG"),//
			Pair.of("java.lang.Long", t -> "LONG"),//
			Pair.of("float", t -> "FLOAT"),//
			Pair.of("java.lang.Float", t -> "FLOAT"),//
			Pair.of("double", t -> "DOUBLE"),//
			Pair.of("java.lang.Double", t -> "DOUBLE"),//
			Pair.of("char", t -> "CHAR"),//
			Pair.of("java.lang.Char", t -> "CHAR"),//
			Pair.of("byte", t -> "BYTE"),//
			Pair.of("java.lang.Byte", t -> "BYTE"),//
			Pair.of("boolean", t -> "BOOLEAN"),//
			Pair.of("java.lang.Boolean", t -> "BOOLEAN"),//
			Pair.of("short", t -> "SHORT"),//
			Pair.of("java.lang.Short", t -> "SHORT"),//
			Pair.of("[]", t -> "ARRAY"));// JAVA type ARRAY

	@Override
	public String paramTypeConvert(String type) {
		type = type.endsWith(ARR_FLAG) ? ARR_FLAG : type;
		return typeConvert.getOrDefault(type, t -> t).apply(type);
	}

	@Override
	public String classConvert(String clazzName) {
		return clazzName;
	}

	@Override
	public boolean isExclude(String type, String method) {
		return CollUtil.contains(javaExcludes.get(type), method);
	}

	@Override
	public String lang() {
		return this.getClass().getSimpleName().toLowerCase();
	}

	@Override
	public boolean staticSupport() {
		return false;
	}
}
